#include "StdAfx.h"
#include "GridParam.h"
#include "EphemWrapper.h"

CGridParam::CGridParam(void)
: m_dStartTime(0)
, m_dEndTime(0)
, m_paramLowBound(0)
, m_paramHiBound(0)
{
}

CGridParam::CGridParam(	double dStartTime, double dEndTime, double paramLowBound, double paramHiBound, double paramRef)
: m_dStartTime(dStartTime)
, m_dEndTime(dEndTime)
, m_paramLowBound(paramLowBound)
, m_paramHiBound(paramHiBound)
, m_paramRef(paramRef)
{
}

CGridParam::~CGridParam(void)
{
}

void CGridParam::Assign(const CGridParam &r )
{
	m_dStartTime = r.m_dStartTime;
	m_dEndTime = r.m_dEndTime;
	m_paramLowBound = r.m_paramLowBound;
	m_paramHiBound = r.m_paramHiBound;

	if(r.m_paramRef != 0.0)
		m_paramRef = r.m_paramRef;

	if(1)
	{
		m_dStartTime += 3600.0;
		m_dEndTime += 500.0;
		double add = (m_paramHiBound - m_paramLowBound)/20;
		m_paramLowBound -= add;
		m_paramHiBound += add;
	}
}

CPoint CGridParam::CalcPoint(CPoint picSize, double dTime, double dParam, double dDist)
{
	CPoint res;
	res.x = getXPos( picSize.x, dParam);
	res.y = getYPos( picSize.y, dTime, dDist);
	return res;
}

bool CGridParam::IsValid() const
{
	return (m_dStartTime < m_dEndTime) && (m_paramLowBound < m_paramHiBound);
}

bool CGridParam::IsIncludedTime(double dTime) const
{
	return (m_dStartTime <= dTime ) && (dTime <= m_dEndTime);
}

bool CGridParam::IsIncludedRange(double dParam) const
{
	return (m_paramLowBound <= dParam ) && (dParam <= m_paramHiBound);
}

double CGridParam::GetParamDiff(double xVal) const
{
	return xVal - m_paramRef;
}

double CGridParam::getXVal(int nWigth, int xPos) const
{
	double res = m_paramLowBound;
	if(nWigth > 0)
		 res = m_paramLowBound + xPos * (m_paramHiBound - m_paramLowBound) / nWigth;

	res = min(res, m_paramHiBound);
	res = max(res, m_paramLowBound);
	return res; 
}

double CGridParam::getYVal(int nHeigth, int yPos) const
{
	double res = m_dStartTime;
	if(nHeigth > 0)
		res = m_dEndTime - yPos * (m_dEndTime - m_dStartTime) / nHeigth;

	res = min(res, m_dEndTime);
	res = max(res, m_dStartTime);
	return res; 
}

int CGridParam::getXPos(int nWigth, double xVal) const
{
	int res = 0;
	if(m_paramLowBound < m_paramHiBound)
		res = int( nWigth * (xVal - m_paramLowBound ) / (m_paramHiBound - m_paramLowBound) );

	res = max(res, 0);
	res = min(res,nWigth);
	return res;
}

int CGridParam::getYPos(int nHeigth, double yVal, double dDist) const
{
	double distRef = 0.2;

	int res = 0;
	if(m_dStartTime < m_dEndTime)
		res = int( nHeigth * (m_dEndTime - yVal ) / (m_dEndTime - m_dStartTime) );

	if(dDist > 0.0)
	{
		dDist = CEphemWrapper::AUtoKM(dDist); // 0 - 10^7
		dDist = max(dDist, 10.0); // 10 - 10^7
		dDist = log10(dDist);  // 1 - 7
		res -= LONG((dDist - 2.0) / 8.0 * nHeigth * distRef);
	}

	res = max(res, 0);
	res = min(res, nHeigth);
	return res;
}